

defense_mob_api.register_mob("mob_botete:botete", {
	hp_max = 8,
	collisionbox = {-0.6,-0.7,-0.6, 0.6,0.4,0.6},
	mesh = "mob_botete_botete.b3d",
	textures = {"mob_botete_botete.png"},
	makes_footstep_sound = false,

	animation = {
		idle = {a=0, b=39, rate=20},
		attack = {a=80, b=99, rate=25},
		move = {a=40, b=79, rate=25},
		move_attack = {a=80, b=99, rate=25},
	},

	smart_path = true,
	mass = 1,
	movement = "air",
	move_speed = 4,
	attack_damage = 0,
	attack_range = 8,
	attack_interval = 16,

	on_step = function(self, dtime)
		local selfpos = self.object:get_pos()
		if self.last_attack_time + self.attack_interval * 0.5 < self.timer or
			self.last_attack_time + 0.25 > self.timer
		then
			local target = self.target
			if target and math.random() < 0.1
			then
				self:attack(target.ref,
					vector.subtract(target.pos, selfpos))
			end
			--self:hunt()
		elseif not self.destination
		then
			self.destination = vector.add(selfpos,
				{x = math.random(-10,10), y = math.random(-5,6), z = math.random(-10,10)})

			self.automatic_face_movement_dir = true
			self.object:set_properties({automatic_face_movement_dir = self.automatic_face_movement_dir})
		end
	end,

	attack = function(self, obj, dir)
		self.last_attack_time = self.timer
		local pos = self.object:get_pos()
		local hdir = vector.normalize({x = dir.x, y = 0, z = dir.z})
		pos = vector.add(pos, vector.multiply(hdir, 0.4))
		local s = 9 -- Launch speed
		local g = -defense_mob_api.gravity

		-- Calculate launch angle
		local angle
		local delta = vector.subtract(obj:get_pos(), pos)
		local x2 = delta.x*delta.x + delta.z*delta.z
		if x2 == 0
		then
			return
		end
		local s2 = s * s
		local r = s2 * s2 - g * (g * x2 + 2 * delta.y * s2)
		if r >= 0
		then
			angle = math.atan((s2 + math.sqrt(r) * (math.random(0, 1) * 2 - 1)) / (g * math.sqrt(x2)))
		else
			angle = math.pi / 4
		end

		-- Calculate initial velocity
		local xs = math.cos(angle) * s * (0.9 + math.random() * 0.2)
		local ys = math.sin(angle) * s * (0.9 + math.random() * 0.2)
		local horiz_angle = math.atan2(delta.z, delta.x) + (math.random() * 0.1 - 0.05)
		local v = {
			x = math.cos(horiz_angle) * xs,
			y = ys,
			z = math.sin(horiz_angle) * xs
		}

		-- Launch projectile
		local projectile = minetest.add_entity(pos, "mob_botete:gooball")
		projectile:setvelocity(v)
		self.object:setvelocity(vector.multiply(v, -0.4))

		self.automatic_face_movement_dir = false
		self.object:set_properties({automatic_face_movement_dir = self.automatic_face_movement_dir})

		if math.random() < 0.1
		then
			self.attack_range = 4 + math.random() * 4
		end
	end,

	on_death = function(self)
		local pos = self.object:get_pos()
		for i = 1, math.random(5, 7)
		do
			local projectile = minetest.add_entity(pos, "mob_botete:gooball")
			projectile:setvelocity({
				x = -2 + math.random() * 4,
				y = -1 + math.random() * 4,
				z = -2 + math.random() * 4
			})
		end
	end,
})


minetest.register_alias("botete", "mob_boteete:botete")
